<template>
    <n-card v-if="true" header-style="background-color:#fafafa" class="my-2 min-h-std" title="策略配置">
        <!-- <n-modal style="width:800px;" class="px-12 pt-8" v-model:show="if_modal" preset="card" title="指标管理">
           <IndicatorManageComponent/>
        </n-modal>

        <template #header-extra>
            <n-button @click="if_modal = true" type="primary">
                指标管理
            </n-button>
        </template> -->
       
        <n-flex vertical class="my-10">
            <n-steps vertical>
                <n-step status="process" title="基本信息">
                    配置回测过程的基本信息，包括选择回测类别、回测初始资金等内容
                    <n-flex justify="center">
                        <n-card class="w-2/3 my-2 py-4" embedded :bordered="false">
                            <BaseInfoConfigComponent :baseinfo_form="form.baseinfo_form" />
                        </n-card>
                    </n-flex>
                </n-step>
                <template v-if="form.baseinfo_form.backtest_type && form.baseinfo_form.scenario_id">
                    <n-step status="process" title="详细配置">
                        根据回测类别进行具体的回测场景设计
                        <n-flex justify="center">
                            <n-card class="w-2/3 my-2 py-4" embedded :bordered="false">
                                <StockInfoConfigComponent @chooseItem="chooseItem" v-if="form.baseinfo_form.backtest_type == 'stock'" :stock_form="form.stock_form"/>
                                <PortfolioInfoConfigComponent :portfolio_form="form.portfolio_form"
                                    v-if="form.baseinfo_form.backtest_type == 'portfolio'" />
                            </n-card>

                        </n-flex>

                    </n-step>
                </template>

                <template v-if="form.baseinfo_form.backtest_type">
                    <n-step status="process" title="策略设计">
                        自定义回测策略，使用语言描述，制定属于你的投资策略
                        <n-flex justify="center">
                            <n-card class="w-2/3 my-2 py-4" embedded :bordered="false">
                                <StrategyInfoConfigComponent :backtest_type="form.baseinfo_form.backtest_type"
                                    :strategy_list="form.strategy_description_list" />
                            </n-card>
                        </n-flex>
                    </n-step>
                </template>
            </n-steps>
        </n-flex>

        <template v-if="current_step == 2 || current_step == 3">
            <n-grid :cols="12">
                <n-gi :span="4">
                    <n-flex justify="center">
                        <span class="text-xl mx-4">
                            {{ scenarioStore.current_scenario.name }}
                        </span>
                    </n-flex>

                </n-gi>
                <n-gi :span="8">
                    <div class="mx-4">
                        <span class="font-bold">简介：</span>{{ scenarioStore.current_scenario.description }}
                    </div>
                    <div class="mx-4">
                        <span class="font-bold">属性关键词：</span><template
                            v-for="i in scenarioStore.current_scenario.attribute_list">
                            {{ i.label }};
                        </template>
                    </div>
                </n-gi>
            </n-grid>
        </template>
{{ form }}
        <n-flex justify="center">
            <n-button type="primary">
                重置
            </n-button>
            <n-button @click="submitForm" type="primary">
                提交
            </n-button>
        </n-flex>
    </n-card>
</template>

<script setup>

import BaseInfoConfigComponent from '@/components/backtest/BaseInfoConfigComponent.vue';
import StockInfoConfigComponent from '@/components/backtest/StockInfoConfigComponent.vue';
import PortfolioInfoConfigComponent from '@/components/backtest/PortfolioInfoConfigComponent.vue';
import StrategyInfoConfigComponent from '@/components/backtest/StrategyInfoConfigComponent.vue';
import IndicatorManageComponent from '@/components/backtest/IndicatorManageComponent.vue';
import { useRouter } from 'vue-router';
import { ref, watch, inject,onMounted } from 'vue';
const if_modal = ref(false);
const router = useRouter();
const chooseItem = (value)=>{
    form.value.stock_form.uuid = value;
}
const form = ref({
    baseinfo_form: {
        scenario_id: "66264ab461ab450c97396973",
        backtest_type: "stock",
        compare_stock_id: "663647b8615cc4eb532a56d5",
        indicator_type: 'type1',
        custom_indicator_list: [],
        cash: 10000,
    },
    stock_form: {
        stock_id: "",

        // stock_id: "66288f3d098315adb8884d27",
        date_range: 15,
        uuid: "5ee5693a-31da-4250-86bb-4e0374d338e0"
    },
    portfolio_form: {
        init_investment: [
            {
                stock_id: '请选择..',
                value: 0
            }
        ]
    },
    strategy_description_list: [{
        description: '如果：该股票收盘价前1日上涨超过1.1%，那么：购买该股票100只'
    }]

})

// const form = ref({
//     baseinfo_form: {
//         scenario_id: null,
//         backtest_type: null,
//         compare_stock_id: null,
//         indicator_type: 'type1',
//         custom_indicator_list: [],
//         cash: null,
//     },
//     stock_form: {

//     },
//     portfolio_form: {
//         init_investment: [
//             {
//                 stock_id: '请选择..',
//                 value: 0
//             }
//         ]
//     },
//     strategy_description_list: [{
//         description: '如果：该股票收盘价前1日上涨超过1.1%，那么：购买该股票100只'
//     }]

// })

import { useScenarioStore } from '@/stores/scenarioStore';
import { useBacktestStore } from '@/stores/backtestStore';
const scenarioStore = useScenarioStore();
const backtestStore = useBacktestStore();
const report = inject('report');
report.value = '';
watch(() => form.value.scenario_id, async (new_value, old_value) => {
    await scenarioStore.getScenarioById(new_value);
})

onMounted(()=>{
    if(backtestStore.form){
        form.value = backtestStore.form;
    }
}
)
const submitForm = async () => {
    await scenarioStore.getScenarioById(form.value.baseinfo_form.scenario_id);
    let backtest_form;
    backtestStore.setForm(form.value);
    if (form.value.baseinfo_form.backtest_type == 'stock') {
        let start_date, end_date, item_start_date, item_end_date;
        for (let i of scenarioStore.current_scenario.item_list) {
            if (i.uuid == form.value.stock_form.uuid) {
                start_date = (i.start_date - form.value.stock_form.date_range * 24 * 60 * 60)
                end_date = (i.end_date + form.value.stock_form.date_range * 24 * 60 * 60);
                item_start_date = i.start_date;
                item_end_date = i.end_date;
            }
        }
        backtest_form = {
            backtest_type: form.value.baseinfo_form.backtest_type,
            scenario_id: form.value.baseinfo_form.scenario_id,
            start_date: start_date,
            end_date: end_date,
            item_uuid: form.value.stock_form.uuid,
            cash: form.value.baseinfo_form.cash,
            stock_id: form.value.stock_form.stock_id,
            indicator_type: form.value.baseinfo_form.indicator_type,
            custom_indicator_list: form.value.baseinfo_form.custom_indicator_list,
            strategy_description_list: form.value.strategy_description_list,
            compare_stock_id: form.value.baseinfo_form.compare_stock_id
        }
    } else if (form.value.baseinfo_form.backtest_type == 'portfolio') {
        backtest_form = {
            backtest_type: form.value.baseinfo_form.backtest_type,
            scenario_id: form.value.baseinfo_form.scenario_id,
            start_date: form.value.portfolio_form.date_range[0] / 1000,
            end_date: form.value.portfolio_form.date_range[1] / 1000,
            cash: form.value.baseinfo_form.cash,
            indicator_type: form.value.baseinfo_form.indicator_type,
            custom_indicator_list: form.value.baseinfo_form.custom_indicator_list,
            init_investment: form.value.portfolio_form.init_investment,
            strategy_description_list: form.value.strategy_description_list,
            compare_stock_id: form.value.baseinfo_form.compare_stock_id
        }
    }

    report.value = await backtestStore.startBacktest(backtest_form);
    router.replace({
        path: "/backtest/analyzer",
    })



}
</script>